﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using RBS.Core.Model;
using RBS.Web.API.Models;

namespace RBS.Web.API.Areas.User.Controllers
{
    /// <summary>
    /// 收藏酒店
    /// </summary>
    public class CollectController : ApiController
    {
        private ApiContext db = new ApiContext();
 
        // GET api/Collect/5
        /// <summary>
        /// 获取用户收藏的酒店
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonData GetU_Collect_Hotel(int id, int page = 1, int limit = 5)
        { 
            var jsonData = new JsonData { Success = true };
            var clients = db.Collect.Where(c => c.UserId == id);
            var items = clients.Select(c =>  
               new{
              c.Id,
              c.CollectTime,
              c.HotelId,
              c.UserId,
              HotelName=db.Hotels.FirstOrDefault(h=>h.H_id==c.HotelId).H_Name
            }
            ); 

            int total = items.Count();
            double pageCount = Math.Ceiling((double)total / (double)limit);
            int totalPage = (int)pageCount;
            if (page > totalPage)
            {
                page = totalPage;
                jsonData.Data = null;
                jsonData.Count = 0;
                jsonData.Page = page;
                jsonData.PageSize = limit;
                jsonData.TotalPage = totalPage;
                jsonData.Success = false;
                jsonData.Error = "最大页数为：" + totalPage + "页";
                return jsonData;
            }
            jsonData.Data = items.OrderByDescending(t => t.CollectTime).Skip((page - 1) * limit).Take(limit);
            jsonData.Count = total;
            jsonData.Page = page;
            jsonData.PageSize = limit;
            jsonData.TotalPage = totalPage;  

            return jsonData; 
        }
        /// <summary>
        /// 添加酒店收藏
        /// </summary>
        /// <param name="u_collect_hotel"></param>
        /// <returns></returns>
        public JsonData PostU_Collect_Hotel(U_Collect_Hotel model)
        { 
            var jsonData = new JsonData {Success = true};

            var items = db.Collect.FirstOrDefault(c => c.UserId == model.UserId && c.HotelId == model.HotelId);
            if (items != null)
            {
                jsonData.Success = false;
                jsonData.Error = "你已经收藏该酒店";
                return  jsonData;
            }


            if (ModelState.IsValid)
            {
                model.CollectTime = DateTime.Now;
                db.Collect.Add(model);
                db.SaveChanges(); 
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, model);  
            }
            else
            {
                jsonData.Success = false;
                jsonData.Error = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState).ToString();

            }
            return jsonData;
        }
/*
        // PUT api/Collect/5
        public HttpResponseMessage PutU_Collect_Hotel(int id, U_Collect_Hotel u_collect_hotel)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != u_collect_hotel.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(u_collect_hotel).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Collect
        public HttpResponseMessage PostU_Collect_Hotel(U_Collect_Hotel u_collect_hotel)
        {
            if (ModelState.IsValid)
            {
                db.Collect.Add(u_collect_hotel);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, u_collect_hotel);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = u_collect_hotel.Id }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
*/
        // DELETE api/Collect/5
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HttpResponseMessage DeleteU_Collect_Hotel(int id)
        {
            var uCollectHotel = db.Collect.Find(id);
            if (uCollectHotel == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            } 
            db.Collect.Remove(uCollectHotel);  
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            } 
            return Request.CreateResponse(HttpStatusCode.OK, uCollectHotel);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}